package com.nononsenseapps.feeder.ui.feed


import android.view.View
import android.widget.ImageView
import androidx.appcompat.content.res.AppCompatResources
import com.nononsenseapps.feeder.ui.R
import com.nononsenseapps.feeder.ui.common.RoundedCornerImageView
import timber.log.Timber
import com.nononsenseapps.feeder.util.*

open class FeedItemHolder_Mini (view: View, actionCallback: ActionCallback, viewModel:Boolean) :AbstractFeedItemHolder(view,actionCallback,viewModel) {

    lateinit var  imageView: RoundedCornerImageView

    override fun initView() {
        super.initView()
        imageView = view.findViewById(R.id.story_image)
        imageView.roundedCorners=RoundedCornerImageView.CORNER_ALL
    }

    override fun initListener() {
        super.initListener()
        imageView.setOnClickListener(View.OnClickListener {
            if(rssItem?.enclosureLink?.isNotEmpty()==true)
                actionCallback.onEnclosureLinkClick(rssItem!!)
            else
                onClick(it)
        })
    }

    override fun resetView() {
        super.resetView()
        imageView.drawable?.let{imageView.setImageDrawable(null)}
    }

    override fun loadImage(){
        val context = view.context
        if (context != null) {
            rssItem?.let { rssItem ->
                try {
                    if(rssItem.imageUrl?.isNotEmpty()==true){
                        loadImage(context, imageView, rssItem.imageUrl!!,rssItem.enclosureLink?.isNotEmpty()==true)
                    }else{
                        AppCompatResources.getDrawable(context, when (PrefUtils.isNightMode(context)) {
                            true -> R.drawable.ic_placeholder_music_white_48dp
                            false -> R.drawable.ic_placeholder_music_night_48dp
                        })?.let {
                            //creageAudioThumb(context, SizedDrawable(it, imageView.width, imageView.height))
                            imageView.setImageDrawable(it)
                        }
                    }

                } catch (e: IllegalArgumentException) {
                    // Could still happen if we have a race-condition?
                    Timber.e( e.localizedMessage)
                }
            }
        }
    }

}